package com.nativetalk.service.three.purchase.insert;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.member.TdNtMemberDetail;
import com.nativetalk.bean.purchase.TdNtMembPurchaseChange;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.CommonUtil;
import com.nativetalk.dao.TdNtMembPurchaseChangeDao;
import com.nativetalk.dao.TdNtMemberDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by lizhun on 16/8/12.
 */
@Service
public class ThreePurchaseInsertServiceImpl implements ThreePurchaseInsertService{
    private static Logger logger = Logger.getLogger(ThreePurchaseInsertServiceImpl.class);
    @Resource
    private TdNtMemberDao tdNtMemberDao;
    @Resource
    private TdNtMembPurchaseChangeDao tdNtMembPurchaseChangeDao;

    @Override
    public @Transactional RetInfo purchase(int memb_id, int purchase_amount) {
        String logInfo = this.getClass().getName() + ":purchase:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtMemberDetail tdNtMember = tdNtMemberDao.findTdNtMemberDetail(memb_id);
            TdNtMembPurchaseChange tdNtMembPurchaseChange = new TdNtMembPurchaseChange();
            tdNtMembPurchaseChange.setMemb_id(memb_id);
            tdNtMembPurchaseChange.setMfchg_change_amount(new BigDecimal(Integer.valueOf(purchase_amount)));
            tdNtMembPurchaseChange.setMfchg_change_month(CommonUtil.getMonth(new Date()));
            tdNtMembPurchaseChange.setMfchg_change_name("充值");
            tdNtMembPurchaseChange.setMfchg_change_type( Constants.FundChangeType.wallet_in);
            tdNtMembPurchaseChange.setState(Constants.PayState.success);
            tdNtMembPurchaseChange.setMfchg_number(CommonUtil.getOrderNub());
            tdNtMembPurchaseChange.setMfchg_back_amount(tdNtMember.getMemb_purchase_balance().add(new BigDecimal(purchase_amount)));
            tdNtMembPurchaseChange.setMfchg_front_amount(tdNtMember.getMemb_purchase_balance());
            tdNtMembPurchaseChangeDao.insertTdNtMembPurchaseChange(tdNtMembPurchaseChange);
            tdNtMember.setMemb_purchase_balance(tdNtMember.getMemb_purchase_balance().add(new BigDecimal(purchase_amount)));
            tdNtMemberDao.updateMemberPurchase(tdNtMember);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("充值成功!");
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
